import { DetailComponent } from './detail/detail.component';
import { DialogService } from './../service/dialog.service';
import { Component, OnInit, ChangeDetectorRef, ViewChild } from '@angular/core';
import { MdPaginator, MdSnackBar, MD_DIALOG_DATA } from "@angular/material";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { DataSource } from '@angular/cdk/collections';
import { ApiServiceService } from '../api.service'
import { MdDialog, MdDialogRef } from '@angular/material';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-stationlog',
  templateUrl: './stationlog.component.html',
  styleUrls: ['./stationlog.component.css']
})
export class StationlogComponent implements OnInit {

  device = "all"
  logType = "all"
  deviceList = [
    { value: 'all', viewValue: '全部' },
    { value: 'EBRPS', viewValue: '平台信息' },
    { value: 'EBRST', viewValue: '台站信息' },
    { value: 'EBRAS', viewValue: '接收设备' },
    { value: 'EBRBS', viewValue: '播放系统' },
    { value: 'EBRDT', viewValue: '播放终端' },
    { value: 'EBMBrdLog', viewValue: '播发记录' },
    { value: 'EBMState', viewValue: '播发状态' }

  ];
  logshowList = [
    { value: 'all', viewValue: '全部' },
    { value: 'req', viewValue: '请求' },
    { value: 'res', viewValue: '反馈' },
  ]
  content = [];
  constructor(private apiService: ApiServiceService,
    private changeDetectorRef: ChangeDetectorRef,
    public snackBar: MdSnackBar,
    public dialog: MdDialog,
    public dialogService: DialogService
  ) {
  }

  stationInfo:Object={};
  stationList:Array<any>;
  stationHead:Array<any>;
  noneText:string='';

  ngOnInit() {

    this.dataSource = new SoundSourceDataSource(this.soundSourceDatabase, this.paginator);
    this.changeDetectorRef.detectChanges();
    this.allSelectedFlag = false;
    this.apiService.getLogs().subscribe(res => {
      if (res.success) {
        this.content = res.data;
        this.soundSourceDatabase.refreshData(this.content);
      }
    })

  }

  displayedColumns = [/*'no',*/ 'EBDID', 'time', 'typeMessage', 'ifRequest', 'operator'];
  soundSourceDatabase = new SoundSourceDatabase();
  dataSource: SoundSourceDataSource | null;
  allSelectedFlag: boolean;

  @ViewChild(MdPaginator) paginator: MdPaginator;

  devTypeChange() {
    if (this.device === "all" && this.logType === "all") {
      this.soundSourceDatabase.refreshData(this.content);
    }
    if (this.device === "all" && this.logType !== "all"){
      let a = this.logType === "req" ? true : false;
      let data = this.content.filter(item => item.ifRequest === a)
      this.soundSourceDatabase.refreshData(data);
    }
    if (this.device !== "all" && this.logType === "all"){
      let data = this.content.filter(item => item.type.indexOf(this.device) === 0)
      this.soundSourceDatabase.refreshData(data);
    }
    if (this.device !== "all" && this.logType !== "all"){
      let a = this.logType === "req" ? true : false;
      let data = this.content.filter(item => item.type.indexOf(this.device) === 0 && item.ifRequest === a)
      this.soundSourceDatabase.refreshData(data);
    }
  }


  detail(row) {
    this.apiService.logDetail(row.EBDID).subscribe(res => {
      console.log(res)
      this.dialogService.openConfirm(DetailComponent, {
        data: res.data
      })

    })
  }

  res(row) {
    this.apiService.resDetail(row.EBDID).subscribe(res => {
      console.log(res)
      this.dialogService.openConfirm(DetailComponent, {
        data: res.data
      })

    })
  }

  openSnackBar(message: string, action: string) {
    this.snackBar.open(message, action, {
      duration: 3000,
    });
  }

}



/*export interface SoundSourceData {
 deleteFlag:boolean;
 no:string;
 acaption:string;
 acontent:string;
 uid:string;
 isChecked:boolean,
 }*/
export interface SoundSourceData {
  // deleteFlag:boolean;
  // no:string;
  EBDID: string;
  time: string;
  typeMessage: string;
  ifRequest: string;
  operator?: any;
}

export class SoundSourceDatabase {
  /** Stream that emits whenever the data has been modified. */
  dataChange: BehaviorSubject<SoundSourceData[]> = new BehaviorSubject<SoundSourceData[]>([]);

  get data(): SoundSourceData[] {
    return this.dataChange.value;
  }


  constructor() {
    // Fill up the database with 100 users.
    var _a: SoundSourceData[] = [
      // {
      //   "deleteFlag": false,
      //   "no": "1",
      //   "acaption": "网络节目源1",
      //   "acontent": "7001",
      //   "uid": "admin",
      //   "isChecked": false
      //
      // }
    ];
    this.dataChange.next(_a);

    // for (var i = 0; i < 100; i++) {
    //   this.addUser();
    // }
  }


  refreshData(array) {
    array.forEach(function (item, index) {
      item.no = index + 1;
    })
    this.dataChange.next(<SoundSourceData[]>array);
  }


}

export class SoundSourceDataSource extends DataSource<any> {
  constructor(private _soundSourceDatabase: SoundSourceDatabase, private _paginator: MdPaginator) {
    super();
  }

  /** Connect function called by the table to retrieve one stream containing the data to render. */
  connect(): Observable<SoundSourceData[]> {
    const displayDataChanges = [
      this._soundSourceDatabase.dataChange,
      this._paginator.page,
    ];
    return Observable.merge(...displayDataChanges).map(() => {
      const data = this._soundSourceDatabase.data.slice();

      // Grab the page's slice of data.
      const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
      return data.splice(startIndex, this._paginator.pageSize);
    });

  }

  disconnect() {
  }
}
